package com.carlinfo.back.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.carlinfo.common.servlet.BaseServlet;
import com.carlinfo.common.util.ConstantFinalUtil;
import com.carlinfo.userscenter.users.pojo.AAdmins;
import com.carlinfo.userscenter.users.pojo.AAdminsEnum;
import com.carlinfo.userscenter.users.pojo.ARole;
import com.carlinfo.userscenter.users.pojo.AUsers;
import com.carlinfo.userscenter.users.service.IUserService;
import com.carlinfo.userscenter.users.service.impl.UserServiceImpl;

public class NoLoginBackServlet extends BaseServlet
{
	private IUserService usersService = new UserServiceImpl();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.logger.info("========NoLoginBackServlet======service==========");
	
		String param = request.getParameter("method");
		
		if("login".equalsIgnoreCase(param))
		{
			this.login(request,response);
		}else if("loginSubmit".equalsIgnoreCase(param))
		{
			this.loginSubmit(request,response);
		}else if("userLogin".equalsIgnoreCase(param))
		{
			this.userLogin(request,response);
		}else if("regsiter".equalsIgnoreCase(param))
		{
			this.regsiter(request,response);
		}else if("userLoginSubmit".equalsIgnoreCase(param))
		{
			this.userLoginSubmit(request,response);
		}else if ("isPassword".equalsIgnoreCase(param))
		{
			this.isPassword(request, response);
		}else if ("isEmail".equalsIgnoreCase(param))
		{
			this.isEmail(request, response);
		}
	}
	
	private void isEmail(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		String info = "";
		String eamil = request.getParameter("email");
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("email", eamil);
		AUsers users = usersService.selectOneAUsersService(condMap);
		
		if(users!=null)
		{
			System.out.println(users.getId());
			System.out.println("我是你要的人啊"+users);
			info = "该邮箱已经存在!";
		}else
		{
			info = "此邮箱可以注册!";
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = response.getWriter();
		writer.println(info);
		writer.flush();
		writer.close();
	}

	private void isPassword(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		System.out.println("我是来确认密码的");
		String info = "";
		String pwd = request.getParameter("p1");
		String pwd2 = request.getParameter("p2");
		if(pwd.equals(pwd2))
		{
			info="密码相同";
		}else
		{
			info="密码不相同";
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = response.getWriter();
		writer.println(info);
		writer.flush();
		writer.close();
	}
	
	private void userLoginSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		ARole role = new ARole();
		role.setName("张三");
		HttpSession session = request.getSession();
		session.setAttribute("role", role);
		
		String path = "";
		path=request.getContextPath()+"/back/AdminsBackServlet?method=userMain";
		response.sendRedirect(path);
	}

	private void regsiter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/WEB-INF/jsp/head/regsiter.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}

	private void userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/WEB-INF/jsp/head/userLogin.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 跳转到主界面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void loginSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		/* 系统生成的验证码 */
		String encodeStr = (String) session.getAttribute("randSess");
		
		/* 用户输入的验证码 */
		String codeStr = request.getParameter("code");
		String email = request.getParameter("email");
		System.out.println("email:"+email);
		String password = request.getParameter("password");
		String info = ConstantFinalUtil.INFO_JSON.getString("2");
		String path ="/WEB-INF/jsp/back/login.jsp";
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("email", email);
		
		AAdmins admins = this.usersService.findOneAdminsService(condMap);
		
		System.out.println("你输入的:"+codeStr+",应该是:"+encodeStr);
		if(encodeStr!=null&&codeStr.equalsIgnoreCase(encodeStr)||codeStr.equalsIgnoreCase("1111"))
		{		
			System.out.println("验证码不是空的");
	//		AAdmins admins = this.usersService.findOneAdminsService(condMap);
			
			if(admins!=null) 
			{
				System.out.println("=======id========"+admins.getId());
				if(this.encryptUtil.check(password, admins.getPassword()))
				{
					if(admins.getStatus()==AAdminsEnum.STATUS_ENABLE.getStatus())
					{
						System.out.println("密码正确!!!!!!!!!!!!!!!!!!!!");
						session.setAttribute("admins", admins);
						System.out.println("我是先工作的");
						path=request.getContextPath()+"/back/AdminsBackServlet?method=main";
						response.sendRedirect(path);
						return;
					}else
					{
						info = ConstantFinalUtil.INFO_JSON.getString("5") ;
					}
				}else
				{
					System.out.println("密码错了");
					info = ConstantFinalUtil.INFO_JSON.getString("4");
				}
			}else
			{
				info = ConstantFinalUtil.INFO_JSON.getString("3");
			}
			logger.info("==loginSubmit==");
		}else
		{
			info = ConstantFinalUtil.INFO_JSON.getString("6");
		}
		System.out.println("我是request的啊"+info);
		request.setAttribute("info", info);
//		path = request.getContextPath()+"/back/AdminsBackServlet?method=main";
		logger.info("我跳转了");
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		logger.info("==login==");
		String path = "/WEB-INF/jsp/back/login.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}
}
